<?php

namespace app\admin\controller\extension;

use app\common\controller\Backend;
use think\db;

/**
 * 多级联动
 *
 * @icon fa fa-table
 * @remark FastAdmin使用了jQuery-cxselect实现多级联动,更多文档请参考https://github.com/karsonzhang/cxSelect
 */
class Index extends Backend
{
    protected $model = null;



    public function _initialize()
    {
        parent::_initialize();
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 查看
     */
    public function index()
    {
        //查询所有游戏
        $gameList = DB::name("game")->select();
        //查询所有小组
        $staffList = DB::name("staff_group")->select();
        //查询录入商品总数
        $yuanlist_temp = array();
        $order_finish = array();
        $order_amount = array();

        if ($this->request->post()) {
            $post = $this->request->post();
            $game_type = $post['game_type1'];
            $game_staff = $post['game_staff'];
            $gamename = $post['gamename1'];
            $gametime = $post['gametime'];
            $staffname = $post['staffname1'];
            $where_post = array();
            if($gamename){
                $where_post['gameid'] = $gamename;
            }else{
                if($game_type != 0){
                    $game_types = $game_type == 1 ? 'network':'mobile';
                    $game_id_list = DB::name("game")->where('type',$game_types)->column("id");
                    $where_post['gameid']=['in',$game_id_list];
                }
            }

            if($gametime){
                $time_arr = explode(' - ',$gametime);
                $time_begin = strtotime($time_arr[0]);
                $time_end = strtotime($time_arr[1]);
                $where_post['a.createtime'] = ['between',[$time_begin,$time_end]];
            }

            if($game_staff == 1){

            }else{

            }
            $where_staff = array();
            if($staffname){
                $staffList_temp = DB::name("staff")->where('group_id',$staffname)->column("invite");
                $where_staff['invite'] = ['in',$staffList_temp];
            }
            //默认当天
            $yuanList = DB::name("staff")->where(array("group_id"=>['<>',0]))->where($where_staff)->field("id,name,invite")->select();
            $yuanList_lest = array_column($yuanList,'invite');

            $goods_temp = DB::name("goods")->alias('a')
                ->join("staff b",'a.invite = b.invite','left')
                ->where("a.invite is not null and a.invite != '' and b.group_id != 0 and a.status = 1")
                ->where(array("a.invite"=>['in',$yuanList_lest]))
                ->where($where_post)
                ->field("a.id,b.name,b.invite,a.createtime")
                ->select();
            foreach($yuanList as $val){
                $yuanlist_temp[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'count'=>0,
                );
                $order_finish[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'count'=>0,
                    'finish'=>0
                );
                $order_amount[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'amount'=>0,
                    'total_fee'=>0
                );
            }

            foreach($goods_temp as $val){
                $yuanlist_temp[$val['invite']]['count'] ++;
            }
            $count_id_list = array_column($yuanlist_temp,'count');
            //二维数组排序
            array_multisort($count_id_list, SORT_DESC, $yuanlist_temp);
            $luru_shangpin = array_values($yuanlist_temp);

            //订单数量
            $goods_id_list = array_column($goods_temp,null,'id');
            $goods_id_list1 = array_column($goods_temp,'id');
            $order_list = DB::name("order")->where(array("goods_id"=>['in',$goods_id_list1]))->field('id,goods_id,amount,status,fee,daifa_fee')->select();
            foreach($order_list as $val){
                $order_finish[$goods_id_list[$val['goods_id']]['invite']]['count'] ++;
                if($val['status'] == 5){
                    $order_finish[$goods_id_list[$val['goods_id']]['invite']]['finish'] ++;
                    $order_amount[$goods_id_list[$val['goods_id']]['invite']]['amount'] += $val['amount'];
                    $order_amount[$goods_id_list[$val['goods_id']]['invite']]['total_fee'] +=( $val['fee'] + $val['daifa_fee']);
                }
            }
            //二维数组排序
            $count_id_list1 = array_column($order_finish,'count');
            array_multisort($count_id_list1, SORT_DESC, $order_finish);
            $dingdanshu = array_values($order_finish);
            //二维数组排序
            $count_id_list2 = array_column($order_finish,'finish');
            array_multisort($count_id_list2, SORT_DESC, $order_finish);
            $chengjiaoshu = array_values($order_finish);
            //二维数组排序
            $count_id_list3 = array_column($order_amount,'amount');
            array_multisort($count_id_list3, SORT_DESC, $order_amount);
            $chengjiaojine = array_values($order_amount);

        }else{
            //默认当天
            $yuanList = DB::name("staff")->where(array("group_id"=>['<>',0]))->field("id,name,invite")->select();
            $yuanList_lest = array_column($yuanList,'invite');
            $goods_temp = DB::name("goods")->alias('a')
                ->join("staff b",'a.invite = b.invite','left')
                ->where("a.invite is not null and a.invite != '' and b.group_id != 0 and a.status = 1")
                ->where(array("a.invite"=>['in',$yuanList_lest]))
                ->field("a.id,b.name,b.invite")
                ->select();
            foreach($yuanList as $val){
                $yuanlist_temp[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'count'=>0,
                );
                $order_finish[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'count'=>0,
                    'finish'=>0
                );
                $order_amount[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'amount'=>0,
                    'total_fee'=>0
                );
            }

            foreach($goods_temp as $val){
                $yuanlist_temp[$val['invite']]['count'] ++;
            }
            $count_id_list = array_column($yuanlist_temp,'count');
            //二维数组排序
            array_multisort($count_id_list, SORT_DESC, $yuanlist_temp);
            $luru_shangpin = array_values($yuanlist_temp);

            //订单数量
            $goods_id_list = array_column($goods_temp,null,'id');
            $goods_id_list1 = array_column($goods_temp,'id');
            $order_list = DB::name("order")->where(array("goods_id"=>['in',$goods_id_list1]))->field('id,goods_id,amount,status,fee,daifa_fee')->select();
            foreach($order_list as $val){
                $order_finish[$goods_id_list[$val['goods_id']]['invite']]['count'] ++;
                if($val['status'] == 5){
                    $order_finish[$goods_id_list[$val['goods_id']]['invite']]['finish'] ++;
                    $order_amount[$goods_id_list[$val['goods_id']]['invite']]['amount'] += $val['amount'];
                    $order_amount[$goods_id_list[$val['goods_id']]['invite']]['total_fee'] +=( $val['fee'] + $val['daifa_fee']);
                }
            }
            //二维数组排序
            $count_id_list1 = array_column($order_finish,'count');
            array_multisort($count_id_list1, SORT_DESC, $order_finish);
            $dingdanshu = array_values($order_finish);
            //二维数组排序
            $count_id_list2 = array_column($order_finish,'finish');
            array_multisort($count_id_list2, SORT_DESC, $order_finish);
            $chengjiaoshu = array_values($order_finish);
            //二维数组排序
            $count_id_list3 = array_column($order_amount,'amount');
            array_multisort($count_id_list3, SORT_DESC, $order_amount);
            $chengjiaojine = array_values($order_amount);
        }

        $where = array();
        // $where['invite'] = array('<>','');
        // //今日时间
        // $now_time_begin = strtotime(date('Y-m-d'));
        // $now_time_end = $now_time_begin+86400;
        // //昨日时间
        // $yestoday_time_begin = strtotime(date('Y-m-d',strtotime("-1 days")));
        // $yestoday_time_end = $yestoday_time_begin + 86400;
        // $where['createtime'] = array("between",[$now_time_begin,$now_time_end]);
        // $info = DB::name("goods")->where('invite','not null')->where($where)->field('id,goods_sn')->select();
        // $now_goods_id_list = array_column($info,'id');
        // $order_info = DB::name("order")->where(array('goods_id'=>['in',$now_goods_id_list]))->count();
        // $order_info_finish = DB::name("order")->where(array('goods_id'=>['in',$now_goods_id_list],'status'=>5))->field('count(*) count,sum(amount) amount')->find();

        // // $where1['invite'] = array('<>','');
        // $where1['createtime'] = array("between",[$yestoday_time_begin,$yestoday_time_end]);
        // $info1 = DB::name("goods")->where('invite','not null')->where($where1)->field('id,goods_sn')->select();
        // $now_goods_id_list1 = array_column($info1,'id');
        // $order_info1 = DB::name("order")->where(array('goods_id'=>['in',$now_goods_id_list1]))->count();
        // $order1_info_finish = DB::name("order")->where(array('goods_id'=>['in',$now_goods_id_list1],'status'=>5))->field('count(*) count,sum(amount) amount')->find();
        
        //本日/昨天
        $now_time_begin = strtotime(date('Y-m-d'));
        $now_time_end = $now_time_begin+86400;
        $yestoday_time_begin = $now_time_begin - 86400;
        $where1['invite'] = array('<>','');
        $where2['invite'] = array('<>','');
        $where1['createtime'] = ['between',[$now_time_begin,$now_time_end]];
        $where2['createtime'] = ['between',[$yestoday_time_begin,$now_time_begin]];
        
        $info = DB::name("goods")->where($where)->where($where1)->field('id,goods_sn')->select();
        $goods_id_list = array_column($info,'id');
        $luru_count = count($info);
        $order_info = DB::name('order')->where(array('goods_id'=>array('in',$goods_id_list)))->count();
        $order_finish_info = DB::name('order')->where(array('goods_id'=>array('in',$goods_id_list),'status'=>5))->field("count(*) count,sum(amount) amount")->find();
        // echo DB::name('order')->getLastSql();die;

        $info1 = DB::name("goods")->where($where)->where($where2)->field('id,goods_sn')->select();
        $goods_id_list1 = array_column($info1,'id');
        $luru_count1 = count($info1);
        $order_info1 = DB::name('order')->where(array('goods_id'=>array('in',$goods_id_list1)))->count();
        $order1_finish_info = DB::name('order')->where(array('goods_id'=>array('in',$goods_id_list1),'status'=>5))->field("count(*) count,sum(amount) amount")->find();
        
        $return_data = array(
            'now_goods'=>count($info),
            'yestoday_goods'=>count($info1),
            'now_order'=>$order_info,
            'now_finish_order'=>$order_finish_info['count'],
            'now_finish_amount'=>$order_finish_info['amount'] ?: 0,
            'yestoday_order'=>$order_info1,
            'yestoday_finish_order'=>$order1_finish_info['count'],
            'yestoday_finish_amount'=>$order1_finish_info['amount'] ?: 0,

        );

        $this->view->assign([
            'gamename'=>$this->request->post('gamename1',0),
            'game_type'=>$this->request->post('game_type1',0),
            'game_staff'=>$this->request->post('game_staff',0),
            'gametime'=>$this->request->post('gametime',''),
            'staffname'=>$this->request->post('staffname1',''),

        ]);

        $this->view->assign([
            'gameList'=>$gameList,
            'staffList'=>$staffList,
            'data'=>$return_data,
            'goods_list'=>$luru_shangpin,
            'dingdanshu'=>$dingdanshu,
            'chengjiaoshu'=>$chengjiaoshu,
            'chengjiaojine'=>$chengjiaojine
        ]);
        return $this->view->fetch();
    }
    
    public function search_info(){
        $game_type = $this->request->post('game_type',0);
        $game_time = $this->request->post('game_time',1);
        $gamename = $this->request->post('gamename');
        $staffname = $this->request->post('staffname');
        $gonghao = $this->request->post('gonghao');
        $yuanlist_temp = array();
        $order_finish = array();
        $order_amount = array();
        $where_post = array();
        
        if($gamename){
                $where_post['gameid'] = $gamename;
            }else{
                if($game_type != 0){
                    $game_types = $game_type == 1 ? 'network':'mobile';
                    $game_id_list = DB::name("game")->where('type',$game_types)->column("id");
                    $where_post['gameid']=['in',$game_id_list];
                }
            }
            
            if($game_time == 2){
                //本周/上周
                $now_time_begin = strtotime('-1 monday', time()) ;
                $now_time_end = time();
                $yestoday_time_begin = strtotime('-1 monday', time()) - 86400*7 ;
                $where1['a.createtime'] = ['between',[$now_time_begin,$now_time_end]];
                $where2['a.createtime'] = ['between',[$yestoday_time_begin,$now_time_begin]];
            }else if($game_time == 3){
                //本月/上月
                $now_time_begin = strtotime(date('Y-m', time()).'-01 00:00:00');
                $now_time_end = time();
                $yestoday_time_begin = strtotime('-1 month', strtotime(date('Y-m', time()).'-01 00:00:00'));
                $where1['a.createtime'] = ['between',[$now_time_begin,$now_time_end]];
                $where2['a.createtime'] = ['between',[$yestoday_time_begin,$now_time_begin]];
            }else{
                //本日/昨天
                $now_time_begin = strtotime(date('Y-m-d'));
                $now_time_end = $now_time_begin+86400;
                $yestoday_time_begin = $now_time_begin - 86400;
                $where1['a.createtime'] = ['between',[$now_time_begin,$now_time_end]];
                $where2['a.createtime'] = ['between',[$yestoday_time_begin,$now_time_begin]];
            }
            
            if($gonghao){
                $where_post['invite'] = $gonghao;
            }else{
                $where_staff = array();
                if($staffname){
                    $staffList_temp = DB::name("staff")->where('group_id',$staffname)->column("invite");
                    $where_staff['invite'] = ['in',$staffList_temp];
                }
            }
            //-------------------本周/月/日
            //默认当天
            $yuanList = DB::name("staff")->where(array("group_id"=>['<>',0]))->where($where_staff)->field("id,name,invite")->select();
            $yuanList_lest = array_column($yuanList,'invite');

            $goods_temp = DB::name("goods")->alias('a')
                ->join("staff b",'a.invite = b.invite','left')
                ->where("a.invite is not null and a.invite != '' and b.group_id != 0 and a.status = 1")
                ->where($where1)
                ->where(array("a.invite"=>['in',$yuanList_lest]))
                ->where($where_post)
                ->field("a.id,b.name,b.invite,a.createtime")
                ->select();
            foreach($yuanList as $val){
                $yuanlist_temp[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'count'=>0,
                );
                $order_finish[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'count'=>0,
                    'finish'=>0
                );
                $order_amount[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'amount'=>0,
                    'total_fee'=>0
                );
            }


            //订单数量
            $goods_id_list = array_column($goods_temp,null,'id');
            $goods_id_list1 = array_column($goods_temp,'id');
            $order_finish = 0;
            $order_amount = 0;
            $order_list = DB::name("order")->where(array("goods_id"=>['in',$goods_id_list1]))->field('id,goods_id,amount,status,fee,daifa_fee')->select();
            foreach($order_list as $val){
                if($val['status'] == 5){
                    $order_finish++;
                    $order_amount += $val['amount'];
                }
            }
            //-------------------上周/月/日
            $goods_temp1 = DB::name("goods")->alias('a')
                ->join("staff b",'a.invite = b.invite','left')
                ->where("a.invite is not null and a.invite != '' and b.group_id != 0 and a.status = 1")
                ->where($where2)
                ->where(array("a.invite"=>['in',$yuanList_lest]))
                ->where($where_post)
                ->field("a.id,b.name,b.invite,a.createtime")
                ->select();
            foreach($yuanList as $val){
                $yuanlist_temp1[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'count'=>0,
                );
                $order_finish1[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'count'=>0,
                    'finish'=>0
                );
                $order_amount1[$val['invite']] = array(
                    'invite'=>$val['invite'],
                    'name'=>$val['name'],
                    'amount'=>0,
                    'total_fee'=>0
                );
            }


            //订单数量
            $goods_id_list = array_column($goods_temp,null,'id');
            $goods_id_list1 = array_column($goods_temp,'id');
            $order_fabu = 0;
            $order_finish = 0;
            $order_amount = 0;
            $order_list = DB::name("order")->where(array("goods_id"=>['in',$goods_id_list1]))->field('id,goods_id,amount,status,fee,daifa_fee')->select();
            foreach($order_list as $val){
                $order_fabu ++;
                if($val['status'] == 5){
                    $order_finish++;
                    $order_amount += $val['amount'];
                }
            }
            
            //订单数量
            $goods_id_list2 = array_column($goods_temp1,null,'id');
            $goods_id_list21 = array_column($goods_temp1,'id');
            $order_fabu1 = 0;
            $order_finish1 = 0;
            $order_amount1 = 0;
            $order_list1 = DB::name("order")->where(array("goods_id"=>['in',$goods_id_list21]))->field('id,goods_id,amount,status,fee,daifa_fee')->select();
            foreach($order_list1 as $val){
                $order_fabu1 ++;
                if($val['status'] == 5){
                    $order_finish1++;
                    $order_amount1 += $val['amount'];
                }
            }
            
        $luru_goods = count($goods_id_list1);
        $luru_goods1 = count($goods_id_list2);
        $return_data = array(
            'code'=>1,
            'luru_num'=>$luru_goods,
            'order_num'=>$order_fabu,
            'order_finish'=>$order_finish,
            'order_amount'=>$order_amount,
            'luru_num_1'=>$luru_goods1,
            'order_num_1'=>$order_fabu1,
            'order_finish_1'=>$order_finish1,
            'order_amount_1'=>$order_amount1,

        );
        echo json_encode($return_data);die;
    }

    // public function search_info(){
    //     $game_type = $this->request->post('game_type',0);
    //     $game_time = $this->request->post('game_time',1);
    //     $gamename = $this->request->post('gamename');
    //     $staffname = $this->request->post('staffname');
    //     $gonghao = $this->request->post('gonghao');
    //     $where = array();
    //     $where['invite'] = array('<>','');
    //     if($gamename){
    //         $where['gameid'] = $gamename;
    //     }else{
    //         if($game_type != 0){
    //             $game_types = $game_type == 1 ? 'network':'mobile';
    //             $game_id_list = DB::name("game")->where('type',$game_types)->column("id");
    //             $where['gameid']=['in',$game_id_list];
    //         }
    //     }
    //     $where1['invite'] = array('<>','');
    //     $where2['invite'] = array('<>','');

    //     if($game_time == 2){
    //         //本周/上周
    //         $now_time_begin = strtotime('-1 monday', time()) ;
    //         $now_time_end = time();
    //         $yestoday_time_begin = strtotime('-1 monday', time()) - 86400*7 ;
    //         $where1['createtime'] = ['between',[$now_time_begin,$now_time_end]];
    //         $where2['createtime'] = ['between',[$yestoday_time_begin,$now_time_begin]];
    //     }else if($game_time == 3){
    //         //本月/上月
    //         $now_time_begin = strtotime(date('Y-m', time()).'-01 00:00:00');
    //         $now_time_end = time();
    //         $yestoday_time_begin = strtotime('-1 month', strtotime(date('Y-m', time()).'-01 00:00:00'));
    //         $where1['createtime'] = ['between',[$now_time_begin,$now_time_end]];
    //         $where2['createtime'] = ['between',[$yestoday_time_begin,$now_time_begin]];
    //     }else{
    //         //本日/昨天
    //         $now_time_begin = strtotime(date('Y-m-d'));
    //         $now_time_end = $now_time_begin+86400;
    //         $yestoday_time_begin = $now_time_begin - 86400;
    //         $where1['createtime'] = ['between',[$now_time_begin,$now_time_end]];
    //         $where2['createtime'] = ['between',[$yestoday_time_begin,$now_time_begin]];
    //     }

    //     if($gonghao){
    //         $where['invite'] = $gonghao;
    //     }else{
    //         if($staffname){
    //             $staffList = DB::name("staff")->where('group_id',$staffname)->column("invite");
    //             $where['invite'] = ['in',$staffList];
    //         }
    //     }

    //     $info = DB::name("goods")->where($where)->where($where1)->field('id,goods_sn')->select();
    //     $goods_id_list = array_column($info,'id');
    //     $luru_count = count($info);
    //     $order_info = DB::name('order')->where(array('goods_id'=>array('in',$goods_id_list)))->count();
    //     $order_finish_info = DB::name('order')->where(array('goods_id'=>array('in',$goods_id_list),'status'=>5))->field("count(*) count,sum(amount) amount")->find();

    //     $info1 = DB::name("goods")->where($where)->where($where2)->field('id,goods_sn')->select();
    //     $goods_id_list1 = array_column($info1,'id');
    //     $luru_count1 = count($info1);
    //     $order_info1 = DB::name('order')->where(array('goods_id'=>array('in',$goods_id_list1)))->count();
    //     $order_finish_info1 = DB::name('order')->where(array('goods_id'=>array('in',$goods_id_list1),'status'=>5))->field("count(*) count,sum(amount) amount")->find();

    //     $return_data = array(
    //         'code'=>1,
    //         'luru_num'=>$luru_count,
    //         'order_num'=>$order_info,
    //         'order_finish'=>$order_finish_info['count'],
    //         'order_amount'=>$order_finish_info['amount'] ?: 0,
    //         'luru_num_1'=>$luru_count1,
    //         'order_num_1'=>$order_info1,
    //         'order_finish_1'=>$order_finish_info1['count'],
    //         'order_amount_1'=>$order_finish_info1['amount'] ?: 0,

    //     );
    //     echo json_encode($return_data);die;
    // }


}
